// 环境变量类型定义
export interface EnvConfig {
  // Node 环境
  NODE_ENV: 'development' | 'production' | 'test';

  // 应用程序配置
  APP_NAME: string;
  APP_VERSION: string;
  APP_PORT: number;
  APP_HOST: string;

  // 数据库配置
  DB_HOST: string;
  DB_PORT: number;
  DB_NAME: string;
  DB_USER: string;
  DB_PASSWORD: string;

  // API 配置
  API_BASE_URL: string;
  API_TIMEOUT: number;
  API_RETRY_COUNT: number;

  // 调试设置
  DEBUG: boolean;
  LOG_LEVEL: 'debug' | 'info' | 'warn' | 'error';
  ENABLE_HOT_RELOAD: boolean;
  ENABLE_SOURCE_MAPS: boolean;

  // 安全配置
  JWT_SECRET: string;
  CORS_ORIGIN: string;

  // 功能开关
  FEATURE_NEW_UI: boolean;
  FEATURE_ANALYTICS: boolean;
  FEATURE_BETA_TESTING: boolean;

  // 第三方服务
  REDIS_URL: string;
  ELASTICSEARCH_URL: string;
}

// 环境变量验证和转换工具
export class EnvValidator {
  /**
   * 获取字符串类型的环境变量
   */
  static getString(key: string, defaultValue?: string): string {
    const value = process.env[key];
    if (!value && !defaultValue) {
      throw new Error(`Environment variable ${key} is required`);
    }
    return value || defaultValue!;
  }

  /**
   * 获取数字类型的环境变量
   */
  static getNumber(key: string, defaultValue?: number): number {
    const value = process.env[key];
    if (!value && defaultValue === undefined) {
      throw new Error(`Environment variable ${key} is required`);
    }
    const parsed = value ? parseInt(value, 10) : defaultValue!;
    if (isNaN(parsed)) {
      throw new Error(`Environment variable ${key} must be a valid number`);
    }
    return parsed;
  }

  /**
   * 获取布尔类型的环境变量
   */
  static getBoolean(key: string, defaultValue?: boolean): boolean {
    const value = process.env[key];
    if (!value && defaultValue === undefined) {
      throw new Error(`Environment variable ${key} is required`);
    }
    if (!value) return defaultValue!;
    return value.toLowerCase() === 'true';
  }

  /**
   * 验证和获取所有环境变量
   */
  static getConfig(): EnvConfig {
    return {
      NODE_ENV: this.getString(
        'NODE_ENV',
        'development'
      ) as EnvConfig['NODE_ENV'],

      APP_NAME: this.getString('APP_NAME', 'TypeScript Demo'),
      APP_VERSION: this.getString('APP_VERSION', '1.0.0'),
      APP_PORT: this.getNumber('APP_PORT', 3000),
      APP_HOST: this.getString('APP_HOST', 'localhost'),

      DB_HOST: this.getString('DB_HOST', 'localhost'),
      DB_PORT: this.getNumber('DB_PORT', 5432),
      DB_NAME: this.getString('DB_NAME', 'tsdemo'),
      DB_USER: this.getString('DB_USER', 'user'),
      DB_PASSWORD: this.getString('DB_PASSWORD', 'password'),

      API_BASE_URL: this.getString('API_BASE_URL', 'http://localhost:3001/api'),
      API_TIMEOUT: this.getNumber('API_TIMEOUT', 5000),
      API_RETRY_COUNT: this.getNumber('API_RETRY_COUNT', 3),

      DEBUG: this.getBoolean('DEBUG', false),
      LOG_LEVEL: this.getString('LOG_LEVEL', 'info') as EnvConfig['LOG_LEVEL'],
      ENABLE_HOT_RELOAD: this.getBoolean('ENABLE_HOT_RELOAD', false),
      ENABLE_SOURCE_MAPS: this.getBoolean('ENABLE_SOURCE_MAPS', false),

      JWT_SECRET: this.getString('JWT_SECRET', 'default_secret'),
      CORS_ORIGIN: this.getString('CORS_ORIGIN', '*'),

      FEATURE_NEW_UI: this.getBoolean('FEATURE_NEW_UI', false),
      FEATURE_ANALYTICS: this.getBoolean('FEATURE_ANALYTICS', false),
      FEATURE_BETA_TESTING: this.getBoolean('FEATURE_BETA_TESTING', false),

      REDIS_URL: this.getString('REDIS_URL', 'redis://localhost:6379'),
      ELASTICSEARCH_URL: this.getString(
        'ELASTICSEARCH_URL',
        'http://localhost:9200'
      ),
    };
  }
}
